package pl.niemar22.unitests;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.TimeUnit;

import javax.xml.ws.handler.MessageContext;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Banan extends SeleniumBase {
	@Test
	public void testBanan() throws Exception {
		driver.get("http://demo.bananascrum.com/");

		assertEquals("Please log in. Type user 'admin' with password 'password'", driver.findElement(By.id("flash")).getText());
		System.out.println("Strona otwarta");
		insertText(By.id("login"), "jhkjh");
		click(By.name("commit"));
		
		assertEquals("Login failed Type user 'admin' with password 'password'",	driver.findElement(By.id("flash")).getText());
		System.out.println("Blad logowanie login");

		insertText(By.id("login"), "admin");
		insertText(By.id("password"), "jhkjh");
		click(By.name("commit"));
		assertEquals("Login failed Type user 'admin' with password 'password'",	driver.findElement(By.id("flash")).getText());
		System.out.println("Blad logowania pass");

		insertText(By.id("login"), "admin");
		insertText(By.id("password"), "password");
		click(By.name("commit"));

		//assertEquals("Logged in successfully", driver.findElement(By.id("flash")) .getText());
		//System.out.println("Logowanie ok");
		WebElement messageContainer = driver.findElement(By.id("flash"));
		assertTrue(messageContainer.getText().contains("Logged in successfully"));
		assertTrue(messageContainer.isDisplayed());
		System.out.println("Logowanie ok");
		
		
		//aktywne czekanie
		
		WebDriverWait wait = new WebDriverWait(driver, 10);
		wait.until(new ExpectedCondition<Boolean>(){
			@Override
			public Boolean apply(WebDriver d) {
				WebElement element =d.findElement(By.id("flash"));
				
				if (element.isDisplayed()){
					System.out.println("Napis Logged in ... jest wyswietlany");
					return null;
				}
				System.out.println("Napis Logged in ... znikl");
				return Boolean.TRUE;
				

			}
		});
		
		
		
	}
}
